import { EntityComponentTypes } from "@minecraft/server";
import { Dimensions } from "@sapi-game/constants";
import { Game, GameComponent } from "@sapi-game/main";
import { Duration, RandomUtils } from "@sapi-game/utils";
import { EffectIds } from "@sapi-game/utils/vanila-data";
import { CvSMainState } from "../mainState";

export class CvSDaemon extends GameComponent<CvSMainState> {
    override onAttach(): void {
        this.subscribe(Game.events.interval, () => {
            this.tpChickenToPlayer();
            this.giveEffectToChickens();
            this.spawnPoint();
            this.checkChickenWin();
        });

        this.subscribe(
            Game.events.interval,
            () => {
                //给玩家效果
                this.context.chicken.forEach((p) =>
                    p.player.addEffect(EffectIds.Invisibility, 100, {
                        showParticles: false,
                    })
                );
                this.killItem();
            },
            new Duration(20)
        );
    }

    tpChickenToPlayer() {
        this.context.playerChickens.forEach((e) => {
            if (e.entity && e.player.player) {
                const rot = e.player.player.getRotation();
                if (rot.x > 40) rot.x = 40;
                if (rot.x < -30) rot.x = -30;
                e.entity.teleport(e.player.player.location, {
                    rotation: rot,
                });
            }
        });
    }

    /**给所有鸡效果 */
    giveEffectToChickens() {
        const allChickens = Dimensions.Overworld.getEntities({
            type: "minecraft:chicken",
            tags: [this.state.chickenTag],
        });
        allChickens.forEach((e) => {
            e.addEffect(EffectIds.InstantHealth, 20, {
                amplifier: 100,
                showParticles: false,
            });
            e.addEffect(EffectIds.Resistance, 100, {
                amplifier: 100,
                showParticles: false,
            });
        });
    }

    spawnPoint() {
        this.context.chicken.forEach((p) => {
            const point = RandomUtils.choice(this.context.map.spreadCenters);
            if (!point) return;
            p.player.setSpawnPoint({
                ...point,
                dimension: Dimensions.Overworld,
            });
        });
        this.context.shepherd.forEach((p) => {
            p.player.setSpawnPoint({
                ...this.context.map.ShepherdSpawnpoint,
                dimension: Dimensions.Overworld,
            });
        });
    }

    checkChickenWin() {
        const players = this.context.map.winArea.getPlayersInRegion();
        if (
            players.some((p) => this.context.chicken.getById(p.id) != undefined)
        ) {
            this.state.end("chicken");
        }
    }

    killItem() {
        const items = this.context.map.region.getEntitesInRegion({
            type: "minecraft:item",
        });
        const eggs = items.filter(
            (i) =>
                i.getComponent(EntityComponentTypes.Item)?.itemStack.typeId ==
                "minecraft:egg"
        );
        eggs.forEach((e) => e.remove());
    }
}
